import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod_test/util/string_util.dart';

import 'cart_item.dart';

class Cart extends ChangeNotifier {
  String id = "";
  List<CartItem> items = [];

  static Cart genCart() {
    Cart cart = Cart();
    cart.id = StringUtil.randomString(length: 6);
    return cart;
  }

  String get displayName {
    String name = "$id :";
    for(final item in items) {
      name += "${item.product.name}(${item.count})";
    }
    return name;
  }

  addItem(CartItem item) {
    items.add(item);
    notifyListeners();
  }

  removeItem(CartItem item) {
    items.remove(item);
    notifyListeners();
  }

  updateItems(List<CartItem> list) {
    items = List.from(list);
    notifyListeners();
  }
}